package com.hanhai.material.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TimeUtils {

    /**
     * 时间格式转化
     * @param format 要转化为的格式
     * @param date 日期
     * @return
     */
    public static String fromDateToString(String format,Date date){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     *
     * @param format
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date fromStringToDate(String format,String date) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        return simpleDateFormat.parse(date);
    }

    /**
     *
     * @param start
     * @param end
     * @param nowDate
     * @return
     */
    public static long getDateInWhatIndex(Date start, Date end, LocalDate nowDate) {
        System.out.println("开始时间："+start);
        System.out.println("结束时间："+end);
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);

        // 起始日期和结束日期
        LocalDate startDate = LocalDate.of(startCalendar.get(Calendar.YEAR), startCalendar.get(Calendar.MONTH) + 1, startCalendar.get(Calendar.DAY_OF_MONTH));
        LocalDate endDate = LocalDate.of(endCalendar.get(Calendar.YEAR), endCalendar.get(Calendar.MONTH) + 1, endCalendar.get(Calendar.DAY_OF_MONTH));
        // 判断日期在一段日期内属于第几天
        long days = ChronoUnit.DAYS.between(startDate, nowDate) + 1; // 注意要加1
        return days;
    }

    /**
     *
     * @param date
     * @return
     */
    public static boolean getDateInWhen(Date date) {
        LocalTime time = LocalTime.of(date.getHours(), date.getMinutes());
        // 判断时间段是上午还是下午
        return time.isBefore(LocalTime.NOON); // 判断时间是否在中午之前
    }

    // 获取某个月的所有日期列表

    /**
     *
     * @param yearMonth
     * @return
     */
    public static List<LocalDate> getDatesOfMonth(YearMonth yearMonth) {
        int lengthOfMonth = yearMonth.lengthOfMonth();
        List<LocalDate> datesOfMonth = new ArrayList<>();
        for (int day = 1; day <= lengthOfMonth; day++) {
            datesOfMonth.add(yearMonth.atDay(day));
        }
        return datesOfMonth;
    }

    /**
     *
     * @param start
     * @param end
     * @return
     */
    public static List<LocalDate> getDatesBetween(Date start, Date end) {
        LocalDate startDate = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        List<LocalDate> dates = new ArrayList<>();
        long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);
        for (long i = 0; i <= numOfDays; i++) {
            LocalDate date = startDate.plusDays(i);
            dates.add(date);
        }
        return dates;
    }
    public static  List<LocalDate> getDatesWeek(){
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        List<LocalDate> localDateList=new ArrayList<>();
        for(int i=0;i<7;i++){
            localDateList.add(calendar.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            calendar.add(Calendar.DAY_OF_WEEK,1);
        }
        return localDateList;
    }

    public static String getWeekDay(LocalDate date) {
        int week_index = date.getDayOfWeek().getValue();
        String[] weekList = {"七", "一", "二", "三", "四", "五", "六","七"};
        return weekList[week_index];
    }
}
